package com.bidevalution.business.function;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.function.FunctionUtils;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.AviatorString;

public class RandomStepFunction extends AbstractFunction {
	private FunctionInitializer functionInitializer;
	public RandomStepFunction(FunctionInitializer functionInitializer) {
		super();
		this.functionInitializer = functionInitializer;
	}
	@Override
	public AviatorObject call(Map<String, Object> env, AviatorObject arg1, AviatorObject arg2, AviatorObject arg3,
			AviatorObject arg4) {
		BigDecimal minBigDecimal = new BigDecimal(arg1.getValue(env).toString());
		BigDecimal maxBigDecimal = new BigDecimal(arg2.getValue(env).toString());
		BigDecimal stepBigDecimal = new BigDecimal(arg3.getValue(env).toString());
		List<BigDecimal> stepValues = new LinkedList<BigDecimal>();
		BigDecimal tempValue = minBigDecimal;
		stepValues.add(tempValue);
		while (tempValue.compareTo(maxBigDecimal) == -1) {
			tempValue = tempValue.add(stepBigDecimal);
			stepValues.add(tempValue);
		}
		return new AviatorString(String.valueOf(stepValues.get(new Random().nextInt(stepValues.size()))));
	}

	@Override
	public String getName() {
		return "randomStep";
	}

}
